﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace System.Windows.JControls.JColorPicker.Tracker
{
    class RGBTracker : ColorTracker
    {
        
        private TrackerRGBModel _rgbModel;        

        public RGBTracker()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
            _rgbModel = TrackerRGBModel.Red;
            SetValue(0, 255, 255);
        }

        public TrackerRGBModel RGBModel
        {
            get { return _rgbModel; }
            set
            {
                _rgbModel = value;
                Invalidate();
            }
        }

        private void SetColorValue(Point mousePoint)
        {
            Rectangle rect = RectangleUtils.RectanglePaint(ClientRectangle, Padding);
            double selectValue = 0;

            if (mousePoint.X < rect.X)
            {
                selectValue = 0;
            }
            else if (mousePoint.X > rect.Right)
            {
                selectValue = 255;
            }
            else
            {
                selectValue = ((double)(mousePoint.X - rect.Left)) * MaxValue / (double)rect.Width;
            }

            if (selectValue > 255)
            {
                selectValue = 255;
            }

            Value = selectValue;
        }

        #region Override Methods

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle rect = RectangleUtils.RectanglePaint(ClientRectangle, Padding);

            Color brushColor = Color.Empty;

            switch (_rgbModel)
            {
                case TrackerRGBModel.Red:
                    brushColor = Color.FromArgb(255, 0, 0);
                    break;
                case TrackerRGBModel.Green:
                    brushColor = Color.FromArgb(0, 255, 0);
                    break;
                case TrackerRGBModel.Blue:
                    brushColor = Color.FromArgb(0, 0, 255);
                    break;
            }

            // Paints tghe color gradiant
            using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(0, 0, 0), brushColor, LinearGradientMode.Horizontal))
            {
                e.Graphics.FillRectangle(brush, rect);
            }

            // Paint the selctor
            PaintSelectors(e.Graphics, rect);
        }

        private void PaintSelectors(Graphics gr, Rectangle rectPaint)
        {
            GraphicsPath grpathUp = GetSignUpPath(rectPaint);

            using (SolidBrush brush = new SolidBrush(Color.Bisque))
            {
                gr.FillPath(brush, grpathUp);
            }

            using (Pen pen = new Pen(Color.Black))
            {
                gr.DrawPath(pen, grpathUp);
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                SetColorValue(e.Location);
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (e.Button == MouseButtons.Left)
            {
                SetColorValue(e.Location);
            }
        } 

        #endregion   
    }

    public enum TrackerRGBModel
    {
        Red,
        Green,
        Blue
    }
}
